<template>
  <div class="loginbody" :style="{ backgroundImage: 'url(' + backImage + ')' }">
    <div class="logindata">
      <div class="logintext">
        <h2>Welcome</h2>
      </div>
      <div class="formdata">
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              clearable
              placeholder="请输入账号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              clearable
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="butt">
        <el-button type="warning" @click="login('form')">登录</el-button>
        <el-button class="shou" @click="register('form')">注册</el-button>
      </div>
    </div>
  </div>
</template>
  
  <script>
import { Login } from "@/api/login";
import { Register } from "@/api/login";
export default {
  name: "LoginView",
  data() {
    return {
      backImage: require("../assets/login" + 1 + ".jpg"),
      form: {
        password: "",
        username: "",
      },
      checked: false,
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { max: 10, message: "不能大于10个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 5, message: "不能小于5个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    login(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          Login(this.form)
            .then((res) => {
              console.log(res);
              if (res.data.code === "200") {
                localStorage.setItem("auth", res.data.data);
                this.$message({
                  message: "登录成功啦",
                  type: "success",
                  showClose: true,
                });
                this.$router.replace("/life");
              } else {
                this.$message({
                  message: res.data.message,
                  type: "error",
                  showClose: true,
                });
              }
            })
            .catch((err) => {
              this.$message({
                message: "账户名或密码错误",
                type: "error",
                showClose: true,
              });
            });
        } else {
          return false;
        }
      });
    },
    register(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          Register(this.form).then((res) => {
            console.log(res);
            if (res.data.code === "200") {
              this.$message({
                message: "注册成功啦",
                type: "success",
                showClose: true,
              });
              this.form = {};
            } else {
              this.$message({
                message: res.data.message,
                type: "error",
                showClose: true,
              });
            }
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>
  
  <style scoped>
.loginbody {
  width: 105%;
  height: 105%;
  background-size: 100% 100%;
  background-position: center center;
  overflow: auto;
  background-repeat: no-repeat;
  position: fixed;
  margin-top: -8px;
  margin-left: -8px;
  /* line-height: 100%; */
  padding-top: 150px;
}

.logintext {
  margin-bottom: 20px;
  line-height: 50px;
  text-align: center;
  font-size: 30px;
  font-weight: bolder;
  color: white;
  text-shadow: 2px 2px 4px #000000;
}

.logindata {
  width: 400px;
  height: 300px;
  transform: translate(-50%);
  margin-left: 50%;
}

.tool {
  display: flex;
  justify-content: space-between;
  color: #606266;
}

.butt {
  margin-top: 10px;
  text-align: center;
}

.shou {
  cursor: pointer;
  color: #606266;
}

/*ui*/
/* /deep/ .el-form-item__label {
    font-weight: bolder;
    font-size: 15px;
    text-align: left;
  }
  
  /deep/ .el-button {
    width: 100%;
    margin-bottom: 10px;
  
  } */
</style>